package com.gengao.springbootcase.controller;

import com.gengao.springbootcase.pojo.Emp;
import com.gengao.springbootcase.pojo.Result;
import com.gengao.springbootcase.service.EmpService;
import com.gengao.springbootcase.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result Login(@RequestBody Emp emp){
        log.info("员工登录，{}",emp);
        Emp e=empService.login(emp);
        System.out.println(emp);
        if(e!=null){
//            下发jwt令牌
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());

            String Jwt= JwtUtils.generateJwt(claims);


            System.out.println(Jwt);
            return Result.success(Jwt);

        }
//        否则返回error
        return Result.error("错误的用户名和密码");



    }
}
